package com.wnx.job.modules.pms.controller;


import com.wnx.job.common.api.Const;
import com.wnx.job.common.api.PageResult;
import com.wnx.job.common.api.QueryPageBean;
import com.wnx.job.common.api.ResultData;
import com.wnx.job.modules.jms.model.JmsCompany;
import com.wnx.job.modules.jms.model.JmsStudent;
import com.wnx.job.modules.jms.service.JmsCompanyService;
import com.wnx.job.modules.jms.service.JmsStudentService;
import com.wnx.job.modules.pms.dto.EmailParam;
import com.wnx.job.modules.pms.dto.SendEmailParam;
import com.wnx.job.modules.pms.model.PmsEmail;
import com.wnx.job.modules.pms.service.PmsEmailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Slf4j
@RestController
@RequestMapping("/pms/pmsEmail")
@Api(tags = "EmailControllerByStu",description = "邮件接收管理")
public class PmsEmailController {

    @Resource
    private JmsStudentService studentService;
    @Resource
    private JmsCompanyService companyService;
    @Resource
    private PmsEmailService emailService;

    /**
     * 查看学生邮件信息
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/getStudentEmail")
    @ApiOperation("查看学生邮件信息")
    public ResultData<PageResult<EmailParam>> getStudentEmail(@RequestBody QueryPageBean queryPageBean, Principal principal) {
        log.info("====系统正在查看学生邮件信息=====");

        //通过认证主体，获取公司账号。
        String username = principal.getName();

        //调用服务层，根据学号获取学生信息
        JmsStudent student = studentService.findByAccount(username);

        //学生ID注入查询条件
        Map<String, Object> queryParams = queryPageBean.getQueryParams();

        if (queryParams == null) {
            queryParams = new HashMap<String, Object>(1);
        }

        queryParams.put("studentId", student.getId());
        queryPageBean.setQueryParams(queryParams);

        //调用服务层，根据学生ID查询邮件集合
        PageResult<EmailParam> pageResult = emailService.getStudentEmail(queryPageBean);

        log.info("====系统成功查看学生邮件信息！！！=====");
        //封装结果集对象返回
        return ResultData.success(Const.QUERY_EMAIL_SUCCESS, pageResult);


    }


    /**
     * 给简历通过人员发送邮件通知
     *
     * @return
     */
    @PostMapping("/sendEmail")
    @ApiOperation("给简历通过人员发送邮件通知")
    public ResultData<String> sendEmail(@Valid @RequestBody SendEmailParam emailDto) {
        //获取前端的邮件Dto

        //构建邮件对象，进行属性赋值
        PmsEmail mail = new PmsEmail();
        mail.setCompanyId(emailDto.getCompanyId());
        mail.setStudentId(emailDto.getStudentId());
        mail.setText(emailDto.getText());
        mail.setToPeople(emailDto.getToPeople());
        mail.setFromPeople("gxuwz0722@163.com");
        mail.setSubject("尊敬的【" + emailDto.getStuName() + "】同学，感谢您应聘【" + emailDto.getCompanyName() + "】的【" + emailDto.getPostName() + "】，您的简历已经通过该公司筛选l啦！");

        //调用服务层，给学生发送简历通过筛选邮件
        emailService.sendMail(mail);


        //返回结果对象
        return ResultData.success(Const.SEND_EMAIL_SUCCESS);
    }


    /**
     * 查看公司的邮件信息
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/getCompanyEmail")
    @ApiOperation("查看公司的邮件信息")
    public ResultData<PageResult<EmailParam>> getCompanyEmail(@RequestBody QueryPageBean queryPageBean, Principal principal) {
        //通过认证主体，获取公司账号。
        String username = principal.getName();

        //调用服务层，根据公司名称获取公司信息
        JmsCompany company = companyService.findByUsername(username);

        //公司ID注入查询条件
        Map<String, Object> queryParams = queryPageBean.getQueryParams();
        if (queryParams == null) {
            queryParams = new HashMap<String, Object>(1);
        }
        queryParams.put("companyId", company.getId());

        queryPageBean.setQueryParams(queryParams);

        //调用服务层，根据公司ID查询邮件集合
        PageResult<EmailParam> pageResult = emailService.getCompanyEmail(queryPageBean);


        //封装结果集对象返回
        return ResultData.success(Const.QUERY_EMAIL_SUCCESS, pageResult);


    }





}

